//
//  LXHomeViewController.swift
//  zhibo
//
//  Created by Echo on 16/9/21.
//  Copyright © 2016年 Echo. All rights reserved.
//

import UIKit

private let kTitleViewH : CGFloat = 40

class LXHomeViewController: UIViewController {
    
    // MARK:- 懒加载属性
    private lazy var pageTitleView : LXPageTitleView = {[weak self] in
        let titleFrame = CGRectMake(0, kNavBarH + kStatusH, kScreenW, kTitleViewH)
        let titles = ["推荐","游戏","娱乐","趣玩"]
        let titleView : LXPageTitleView = LXPageTitleView(frame: titleFrame, titles: titles)
        titleView.delegate = self
        return titleView
    }()
    
    private lazy var pageContentView : LXPageContentView = {
        let contentH : CGFloat = kScreenH - kNavBarH - kTabBarH - kStatusH - kTitleViewH
        let contentFrame = CGRectMake(0, kNavBarH + kStatusH + kTitleViewH, kScreenW, contentH)
        var childVCs = [UIViewController]()
        childVCs.append(LXRecommendViewController())
        
        for _ in 0..<3 {
            let vc = UIViewController()
            vc.view.backgroundColor = UIColor(r: CGFloat(arc4random_uniform(255)), g:CGFloat(arc4random_uniform(255)), b: CGFloat(arc4random_uniform(255)))
            childVCs.append(vc)
        }
        
        let contentView = LXPageContentView(frame: contentFrame, childVCs: childVCs, parentVC: self)
        contentView.delegate = self
         
         return contentView
        
    }()

    
    // MARK:- 系统回调
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
    }
}

extension LXHomeViewController {
    private func setupUI(){
        // 不需要调整scrollV的内边距
        automaticallyAdjustsScrollViewInsets = false
        
        setupNav()
        
        // 添加子控件
        view.addSubview(pageTitleView)
        
        view.addSubview(pageContentView)
    }
    
    private func setupNav(){
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "logo")
        
        let size = CGSizeMake(40, 40)
        let historyItem = UIBarButtonItem(imageName: "image_my_history", highImageName: "Image_my_history_click", size: size)
        let searchItem = UIBarButtonItem(imageName: "btn_search", highImageName: "btn_search_clicked", size: size)
        let qrCodeItem = UIBarButtonItem(imageName: "Image_scan", highImageName: "Image_scan_click", size: size)
        navigationItem.rightBarButtonItems = [historyItem,searchItem,qrCodeItem]
        
    }
}

extension LXHomeViewController: PageTitleViewDelegate {
    
    func pageTitleViewSelected(titleView: LXPageTitleView, selectedIndex index: Int) {
        pageContentView.setCurrentIndex(index)
    }
}

extension LXHomeViewController : PageContentViewDelegate {
    func pageContentViewScrolled(contentView: LXPageContentView, progress: CGFloat, sourceIndex: Int, targetIndex: Int) {
        pageTitleView.setTitleWithProgress(progress, sourceIndex: sourceIndex, targetIndex: targetIndex)
    }
    
}
